/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 23735
 * Date: 2022-10-10
 * Time: 17:30
 */

class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}
public class Solution {
    public ListNode reverseList(ListNode head) {
        if(head==null||head.next==null){
            return head;
        }
        ListNode prev=head;
        ListNode cur=head.next;
        ListNode next=cur.next;
        prev.next=null;
        while(next!=null){
            cur.next=prev;
            prev=cur;
            cur=next;
            next=next.next;
        }
        cur.next=prev;
        return cur;

    }
    public boolean isPalindrome(ListNode head) {
        if(head==null||head.next==null){
            return true;
        }
        ListNode fast = head;
        ListNode slow = head;
        while(fast!=null&&fast.next!=null){
            fast=fast.next.next;
            slow=slow.next;
        }
        ListNode tail=reverseList(slow);
        ListNode temp=head;
        while(tail!=null){
            if(temp.val!=tail.val){
                return false;
            }
            tail=tail.next;
            temp=temp.next;
        }
        return true;
    }
}
